<template>
  <div ref="myChart1" style="width: 700px;height:560px;" />
</template>

<script>
import userApi from '@/api/user'

export default {
  data() {
    return {
      rankingIds: {},
      booksName: [],
      booksSale: []
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.setChart()
    })
  },
  methods: {
    async setChart() {
      const myChart = this.$echarts.init(this.$refs.myChart1)
      await userApi.ranking().then((resp) => {
        this.rankingIds = Object.entries(resp.data.data.rank)
        this.rankingIds.sort((a, b) => { return b[1] - a[1] })
      }, err => {
        console.log(err)
      })
      for (let i = 0; i < this.rankingIds.length; i++) {
        this.booksSale.push(this.rankingIds[i][1])
        await userApi.getBookById(this.rankingIds[i][0].toString()).then(resp => {
          this.booksName.push(Object.values(resp.data.data)[0]['bookName'])
        },
        err => {
          console.log(err)
        })
      }
      this.booksSale.sort((a, b) => { return b - a })
      myChart.setOption({
        title: {
          text: 'ECharts 销量柱状图'
        },
        tooltip: {},
        legend: {
          data: ['销量']
        },
        xAxis: {
          data: this.booksName
        },
        yAxis: {},
        series: [{
          name: '销量',
          type: 'bar',
          data: this.booksSale,
          itemStyle: {
            color: function(params) {
              const colorList = ['#00FFFF', '#C0C0C0', '#66FF99', '#000000', '#FF0000']
              return colorList[params.dataIndex]
            }
          }
        }]
        // color: ['blue']
      })

      // console.log(bookName)
      // console.log(arrName)
      // console.log(arrVal)
    }

  }
}
</script>

<style scoped>

</style>
